/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.psp.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.cspimp.psp.domain.OmSurrounding;
import me.zhengjie.modules.cspimp.psp.service.OmSurroundingService;
import me.zhengjie.modules.cspimp.psp.service.dto.OmSurroundingQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zmm
 * @website https://docs.auauz.net
 * @date 2020-05-30
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "周边环境管理")
@RequestMapping("/api/omSurrounding")
public class OmSurroundingController {

    private final OmSurroundingService omSurroundingService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('omSurrounding:list')")
    public void download(HttpServletResponse response, OmSurroundingQueryCriteria criteria) throws IOException {
        omSurroundingService.download(omSurroundingService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询周边环境")
    @ApiOperation("查询周边环境")
    @PreAuthorize("@el.check('omSurrounding:list')")
    public ResponseEntity<Object> query(OmSurroundingQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(omSurroundingService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增周边环境")
    @ApiOperation("新增周边环境")
    @PreAuthorize("@el.check('omSurrounding:add')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unitDesc", value = "单位概述", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "naturalPosition", value = "地理位置与自然条件", dataType = "String", paramType = "body"),
    })
    public ResponseEntity<Object> create(@Validated @RequestBody OmSurrounding resources) {
        return new ResponseEntity<>(omSurroundingService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改周边环境")
    @ApiOperation("修改周边环境")
    @PreAuthorize("@el.check('omSurrounding:edit')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", required = true, paramType = "body"),
            @ApiImplicitParam(name = "unitDesc", value = "单位概述", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "naturalPosition", value = "地理位置与自然条件", dataType = "String", paramType = "body"),
    })
    public ResponseEntity<Object> update(@Validated @RequestBody OmSurrounding resources) {
        omSurroundingService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除周边环境")
    @ApiOperation("删除周边环境")
    @PreAuthorize("@el.check('omSurrounding:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        omSurroundingService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}